package com.eshop.we.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eshop.we.business.LeaseOrderBusiness;
import com.eshop.we.constants.BaseConstants;
import com.eshop.we.leaseorder.entity.LeaseOrder;
import com.eshop.we.result.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author wangzhanguo
 * @create 2018-06-05 10:54
 **/
@Api(value = "租书接口管理", description = "租书接口管理")
@RestController
@RequestMapping("/leaseorder")
public class LeaseOrderController extends BaseController{

    @Autowired
    private LeaseOrderBusiness leaseOrderBusiness;

    /**
     * 租书收益列表
     * @return
     */
    @GetMapping("/income")
    public Result income(Page<LeaseOrder> page){
       return Result.success(leaseOrderBusiness.selectIncomePage(page));
    }

    /**
     * 租书列表
     * @return
     */
    @GetMapping("/lease")
    public Result lease(Page<LeaseOrder> page){
        return Result.success(leaseOrderBusiness.selectLeasePage(page));
    }



    @ApiOperation(value = "借入/借出管理",notes = "借入/借出管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", required = true, dataType = "Integer", paramType = "query", value = "状态 借出中 0 记录 1"),
            @ApiImplicitParam(name = "type", required = true, dataType = "Integer", paramType = "query", value = "借入借出类型行 0 借入 1 借出"),
    })

    @GetMapping("getleaselist")
     public Result getLeaseList(@RequestParam(required = true)Integer status, @RequestParam(required = true)Integer type,  @ApiIgnore @RequestParam(required = true)Integer userId){
        Integer providerId=null; //出租人id
        Integer consumerId=null;//租书人id
        Integer rent_del=null;//出租方
        Integer is_del=null;//租书方
        switch (type){
            case 0:
                providerId=userId;
                rent_del=0;
                break;
            case 1:
                consumerId=userId;
                is_del=0;
                break;
            default:
                break;
        }
        Integer outstatus=null;
        Integer oldstatus=null;
        if(status!=null && status==0){
            outstatus=50;
        }else {
            oldstatus=50;
        }
        EntityWrapper ew=new EntityWrapper<LeaseOrder>();
        if(outstatus!=null){ew.ne("status",outstatus);};
        if(oldstatus!=null){ew.eq("status",oldstatus); }
        if(providerId!=null){ew.eq("provider_id",providerId);};
        if(consumerId!=null){ew.eq("consumer_id",consumerId);};
        if(is_del!=null){ew.eq("is_del",is_del);};
        if(rent_del!=null){ew.eq("rent_del",rent_del);};
        return Result.success(leaseOrderBusiness.selectList(ew));
     }

    @ApiOperation(value = "申请还书接口",notes = "申请还书接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "leaseId", required = true, dataType = "Integer", paramType = "query", value = "订单id")
    })
    @ApiResponses({
            @ApiResponse(code=2000,message="订单状态错误")
    })

    @PutMapping("/applyback")
    public Result updateReturnStatus(@RequestParam(required = true)Integer leaseId){
        LeaseOrder leaseOrder=new LeaseOrder();
        leaseOrder.setId(leaseId);
        leaseOrder=leaseOrderBusiness.selectById(leaseOrder);
        if(leaseOrder.getStatus()!=30){
            return new Result(BaseConstants.ORDER_STATUS_ERROR,"订单状态错误",null);
        }
        leaseOrder.setStatus(40);
        return Result.success(leaseOrderBusiness.updateById(leaseOrder));
    }

    @ApiOperation(value = "确认还书",notes = "确认还书")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "leaseId", required = true, dataType = "Integer", paramType = "query", value = "订单id")
    })
    @ApiResponses({
            @ApiResponse(code=2000,message="订单状态错误")
    })
    @PutMapping("/affirmbook")
    public Result updateAffirmStatus(@RequestParam(required = true)Integer leaseId){
        LeaseOrder leaseOrder=new LeaseOrder();
        leaseOrder.setId(leaseId);
        leaseOrder=leaseOrderBusiness.selectById(leaseOrder);
        if(leaseOrder.getStatus()!=40){
            return new Result(BaseConstants.ORDER_STATUS_ERROR,"订单状态错误",null);
        }
        leaseOrder.setStatus(50);
        return Result.success(leaseOrderBusiness.updateById(leaseOrder));
    }

    @ApiOperation(value = "借入借出删除订单",notes = "借入借出删除订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "leaseId", required = true, dataType = "Integer", paramType = "query", value = "订单id"),
            @ApiImplicitParam(name = "type", required = true, dataType = "Integer", paramType = "query", value = "借入借出类型行 0 借入 1 借出")
    })
    @ApiResponses({
            @ApiResponse(code=2000,message="订单状态错误")
    })
    @PutMapping("/delectlease")
    public Result delectLease(@RequestParam(required = true)Integer leaseId,@RequestParam(required = true)Integer type){
        LeaseOrder leaseOrder=new LeaseOrder();
        leaseOrder.setId(leaseId);
        leaseOrder=leaseOrderBusiness.selectById(leaseOrder);
        if(leaseOrder.getStatus()!=50){
            return new Result(BaseConstants.ORDER_STATUS_ERROR,"订单状态错误",null);
        }
        if(type==0){//借入
            leaseOrder.setIsDel(1);
        }else {
            leaseOrder.setRentDel(1);
        }
        return Result.success(leaseOrderBusiness.updateById(leaseOrder));
    }

}
